package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbContentMapper;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.service.ContentService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@DubboService
public class ContentServiceImpl implements ContentService {

    @Resource
    private TbContentMapper tbContentMapper;
    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public List<TbContent> selectInfo() {
        List<TbContent> list = tbContentMapper.selectInfo();
        return list;
    }

    @Override
    public void insertInfo(TbContent tbContent) {
        tbContentMapper.insertSelective(tbContent);
    }

    @Override
    public TbContent toUpdate(Long id) {

        return tbContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateInfo(TbContent tbContent) {
        redisTemplate.delete("dongdongshop");
        tbContentMapper.updateByPrimaryKeySelective(tbContent);
    }

    @Override
    public void deleteById(Long[] ids) {
        tbContentMapper.deleteById(ids);
        redisTemplate.delete("dongdongshop");
    }

    @Override
    public List<TbContent> selectByCotegoryId() {
        String key = "dongdongshop";
        String filed = "content:info1";
        if (redisTemplate.opsForHash().hasKey(key,filed)){
            List<TbContent> contents = (List<TbContent>) redisTemplate.opsForHash().get(key, filed);
            return contents;
        }else {
            List<TbContent> list = tbContentMapper.selectByCotegoryId();
            redisTemplate.opsForHash().put(key,filed,list);
            return list;
        }
    }

    @Override
    public List<TbContent> tuijianList() {
        String key = "dongdongshop";
        String filed = "content:info2";
        if (redisTemplate.opsForHash().hasKey(key,filed)){
            List<TbContent> contents = (List<TbContent>) redisTemplate.opsForHash().get(key, filed);
            return contents;
        }else {
            List<TbContent> list = tbContentMapper.tuijianList();
            redisTemplate.opsForHash().put(key,filed,list);
            return list;
        }
    }

    @Override
    public List<TbContent> xihuanList() {
        String key = "dongdongshop";
        String filed = "content:info3";
        if (redisTemplate.opsForHash().hasKey(key,filed)){
            List<TbContent> contents = (List<TbContent>) redisTemplate.opsForHash().get(key, filed);
            return contents;
        }else {
            List<TbContent> list = tbContentMapper.xihuanList();
            redisTemplate.opsForHash().put(key,filed,list);
            return list;
        }
    }
}
